package com.ohuang.jiaxiaotong.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.ohuang.jiaxiaotong.Activity.AddCourseActivity;
import com.ohuang.jiaxiaotong.Adapter.CourseTableRvAdapter;
import com.ohuang.jiaxiaotong.Adapter.CourseTableRvAdapter2;
import com.ohuang.jiaxiaotong.MyHttpURL;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.EventMsgCode;
import com.ohuang.jiaxiaotong.base.LazyFragment;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.Timetable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;


public class CourseTable2Fragment extends LazyFragment {

    private RecyclerView recyclerView;
    private CourseTableRvAdapter2 courseTableRvAdapter2;
    private ArrayList<String> mData;
    private CheckBox rv_checkBox;
    private TextView textView_week;
    private int txt_week_res[] = {R.id.txt_we1, R.id.txt_we2, R.id.txt_we3, R.id.txt_we4, R.id.txt_we5};
    private  String className;

    public CourseTable2Fragment() {
        // Required empty public constructor
    }
    public CourseTable2Fragment(String className) {
        // Required empty public constructor
        this.className=className;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }



    @Override
    protected void initView(View root) {
        initView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_course_table2;
    }

    @Override
    public void startLoad() {
        HttpGetAllTimetable();//获取课程
    }

    private void initView(){
        recyclerView=view.findViewById(R.id.rv_courseTable2);
        java.util.Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday < 7&&weekday>1) {
            textView_week = view.findViewById(txt_week_res[weekday-2]);
            textView_week.setBackgroundResource(R.drawable.dra_txt_week);
        }

        final GridLayoutManager gridLayoutManager
                = new GridLayoutManager(getContext(), 5);
        recyclerView.setLayoutManager(gridLayoutManager);
        mData = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 5; j++) {
                mData.add("");
            }
        }
        courseTableRvAdapter2 = new CourseTableRvAdapter2(getContext(), mData);
        recyclerView.setAdapter(courseTableRvAdapter2);
    }


    private void HttpGetAllTimetable() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyHttpURL.mainUrl )
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        GithubService service = retrofit.create(GithubService.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("className", className);

        service.getAllTimetable(map).enqueue(new Callback<ArrayList<Timetable>>() {
            @Override
            public void onResponse(Call<ArrayList<Timetable>> call, Response<ArrayList<Timetable>> response) {
                ArrayList<Timetable> timetableList = response.body();
                assert timetableList != null;
                for (Timetable timetable : timetableList) {
                    int week = timetable.getWeek()-1;
                    int c = timetable.getCourse()-1;
                    int pos = week + c * 5;
                    if ("".equals(timetable.getTeacher())){
                        mData.set(pos, timetable.getGrade());
                    }else {
                        mData.set(pos, timetable.getGrade()+"\n@"+timetable.getTeacher());
                    }
                }
                courseTableRvAdapter2.setMData(mData);
                courseTableRvAdapter2.notifyDataSetChanged();

            }

            @Override
            public void onFailure(Call<ArrayList<Timetable>> call, Throwable t) {

            }
        });
    }



    interface GithubService {
        //https://api.github.com/repos/{owner}/{repo}/contributors
        @POST("user/getAllClassTimetable")
        Call<ArrayList<Timetable>> getAllTimetable(@QueryMap HashMap<String, String> deviceInfo);
    }


}
